import com.alibaba.fastjson.{JSON, JSONObject}
import com.o2o.utils.Iargs
import org.apache.spark.sql.SparkSession
import org.elasticsearch.spark._
/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/6/5 16:23
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object InsertESMiyaData {
  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name","O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", Iargs.OBSACCESS)
    sc.hadoopConfiguration.set("fs.s3a.secret.key", Iargs.OBSSECRET)
    sc.hadoopConfiguration.set("fs.s3a.endpoint", Iargs.OBSENDPOINT)
    sc.setLogLevel("WARN")

    val index = "2020_rongyigou/rongyigou_2020_9"

    sc.esJsonRDD(index,
      """
        |
        |{
        |  "query": {
        |    "match_phrase": {
        |      "good_id": "9001862469"
        |    }
        |  }
        |}
      """.stripMargin).values.map(line => {
      val nObject: JSONObject = JSON.parseObject(line.toString())

      nObject.put("firstCategoryId","10016")
      nObject.put("secondCategoryId","1001605")
      nObject.put("thirdCategoryId","100160599")
      nObject.put("fourthCategoryId","10016059999")

      nObject
    }).saveToEs(index, Map("es.mapping.id" -> "good_id"))


  sc.stop()
}
}
